package org.qiuqiu.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.qiuqiu.service.impl.PostServiceImpl;
import org.qiuqiu.utils.DateUtil;
import org.qiuqiu.vo.PaginationVO;
import org.qiuqiu.vo.Post;
import org.qiuqiu.vo.PostExample;
import org.qiuqiu.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

@Controller
@RequestMapping("/post")
public class PostController {

	@Autowired
	private PostServiceImpl service;

	// 发布新帖
	@RequestMapping(value = "newpost")
	public void newPost(@RequestParam("post_tile") String post_title,
			@RequestParam("post_content") String post_content,
			HttpSession session, HttpServletResponse rs, Model model) {
		String flag = "no";
		Gson gson = new GsonBuilder().create();
		rs.setContentType("application/json; charset=utf-8");
		PrintWriter out = null;
		try {
			out = rs.getWriter();
			User user = (User) session.getAttribute("user");
			if (user.getStatus() == 0) {
				flag = "personal";
			} else {
				Post post = new Post();
				post.setPostContent(post_content);
				post.setPostTitle(post_title);
				post.setPostCreatetime(new Date());
				post.setPostUsername(user.getNickname());
				post.setUserid(user.getUserid());
				post.setPostResponse(0);
				service.insert(post);
				flag = "" + post.getPostid();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.write(gson.toJson(flag));
			out.flush();
			out.close();
		}

	}

	public String getPostById(int postId, Model model) throws Exception {
		Post post = service.selectByPrimaryKey(postId);
		model.addAttribute("post", post);
		return "content";
	}

	// 得到所有贴子
	@RequestMapping(value = "/all")
	public String getAllPost(Model model, PaginationVO page) throws Exception {

		List<Post> postArrayList = service.findallClickandInfo(page);
		int total = service.totalInfo();
		page.setTotalField(total);
		model.addAttribute("postArrayList", postArrayList);
		model.addAttribute("page", page);
		return "index";
	}

	// 删帖子
	@RequestMapping(value = "/delpost",method=RequestMethod.POST)
	public void delPost(@RequestParam("postid") int postid,
			@RequestParam("userid") int userid, HttpServletResponse rs) {
		String flag = "no";
		Gson gson = new GsonBuilder().create();
		rs.setContentType("application/json; charset=utf-8");
		PrintWriter out = null;
		try {
			out = rs.getWriter();
			service.deleteByPrimaryKey(postid);
			flag = "ok";
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			out.write(gson.toJson(flag));
			out.flush();
			out.close();
		}

	}
}
